var app = getApp();
var util = require('../../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price_type:2,
    choosemodel: ["固定价", '时价'],
    showModalStatus: false,  //底部分类弹窗
    xselectdingjia: true, //定价模式下拉框
    value: [0, 0],  //底部弹窗初始值
    specs_deatail:''
  },
  // 保存
  baochu: function () {
    var that = this;
    console.log(that.data.price_type);
    console.log(that.data.unitindex);
    console.log(that.data.unit_name);
    //验证第一个字符串
    var regNumber = /[A-Za-z]/;
    var typestr = typeof this.data.str;
    if (typestr == "string") {
      var str = this.data.str;
    } else {
      var str = this.data.str.join('');
    }
    var str1 = ''
    for (var i in str) {
      if (regNumber.test(str[i])) {
        str1 += str[i];
      }
    }
    if (that.data.one_class ==0 && that.data.two_class==0){
      wx.showToast({
        title: '请选择分类'
      })
    }else{
      app.AppHttp('api/Supplier/addBasicToGoods', {
        supplier_id: app.globalData.loginInfo.id,
        cover: that.data.cover,
        trade_name: that.data.trade_name.trim(),
        unit: that.data.unitindex,
        trade_alias: that.data.trade_alias.trim(),
        one_class: that.data.one_class,
        two_class: that.data.two_class,
        commodity_code: that.data.commodity_code,
        price_type: that.data.price_type,
        true_price: that.data.true_price,
        abbreviation: str1.trim(),
        specs: that.data.specs_deatail,
        is_show_detail: that.data.is_show_detail,
        e_type:'client'
      }, function (res) {
        console.log(that.data.true_price)
        console.log(JSON.stringify(res.data));
        app.globalData.shop_manage=1;
        if (res.data.status != -1) {
          wx.showToast({
            title: res.data.msg
          })
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }  
  },
  //输入规格详情
  specsInput: function (e) {
    this.setData({
      specs_deatail: e.detail.value
    })
  },
  // 输入别名
  ordernameinput: function (e) {
    this.setData({
      trade_alias: e.detail.value
    })
  },
  // 输入拼写
  pinxieinput: function (e) {
    console.log(e.detail.value);
    this.setData({
      str: e.detail.value
    })
  },
  // 点击所属分类弹窗
  fenleick: function () {
    util.showBuyModal(this);
    this.setData({
      showModalStatus: true
    })
    if (this.data.notwo == 1) {
      this.startcatelist();
    }
  },
  // 关闭所属分类弹窗
  closefenlei: function () {
    util.hideBuyModal(this,"showModalStatus");
  },
  // 确定并关闭所属分类弹窗
  fenleiconfirm: function () {
    if (this.data.notwo == 1) {
      wx.showToast({
        title: '没有二级分类'
      })
      return;
    }
    util.hideBuyModal(this,"showModalStatus");
    this.setData({
      one_class_name: this.data.one_class_zhname,
      two_class_name: this.data.two_class_zhname,
      one_class: this.data.one_class_zh,
      two_class: this.data.two_class_zh
    })
  },
  // 选择所属分类
  bindChange: function (e) {
    var that = this;
    var val = e.detail.value;
    console.log(val);
    var one_class_list = this.data.one_class_list;
    var two_class_list = this.data.two_class_list;
    app.AppHttp('api/Supplier/CateList', {
      id: app.globalData.loginInfo.id,
      one_id: one_class_list[val[0]].private_one_class,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data));
      if (val[1] > res.data.two_class_list.length - 1) {
        val[1] = res.data.two_class_list.length - 1
      }
      if (res.data.two_class_list.length == 0) {
        that.setData({
          two_class_list: [''],
          notwo: 1   
        })
        return;
      }
      that.setData({
        one_class_list: res.data.one_class_list,
        two_class_list: res.data.two_class_list,
        one_class_zhname: res.data.one_class_list[val[0]].private_class_name,
        two_class_zhname: res.data.two_class_list[val[1]].private_class_name,
        one_class_zh: res.data.one_class_list[val[0]].private_one_class,
        two_class_zh: res.data.two_class_list[val[1]].private_one_class,
        value: val,
        notwo: 0
      });
    })
  },
  // 点击定价模式弹窗
  setdingjia: function (e) {
    this.setData({
      xselectdingjia: false
    })
  },
  //点击定价模式切换
  mySelectdingjia: function (e) {
    this.setData({
      dingjia: e.target.dataset.me,
      xselectdingjia: true
    })
    if (this.data.dingjia == '固定价') {
      this.setData({
        price_type: 2,
        true_price: ''
      })
    } else if (this.data.dingjia == '时价') {
      this.setData({
        price_type: 1
      })
    }

  },
  // 输入真实价格
  shopmoneyinput: function (e) {
    this.setData({
      true_price: e.detail.value
    })
  },
  // 是否显示详情
  show_detail:function(){
    var is_show_detail=this.data.is_show_detail;
    if (is_show_detail==0){
      is_show_detail=1
    }else{
      is_show_detail = 0
    }
    this.setData({
      is_show_detail: is_show_detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      public_id: options.public_id,  //当前公共菜品的id
      is_count: options.is_count
    });
    this.uplist();
    this.startcatelist();
  },
  uplist: function () {
    var that = this;
    app.AppHttp('api/Supplier/getBasicFind', {
      // openid: app.globalData.openid,
      id:that.data.public_id,
      supplier_id: app.globalData.loginInfo.id,
      is_count: that.data.is_count
    }, function (res) {
      console.log(JSON.stringify(res.data));
      that.setData({
        trade_name: res.data.trade_name,
        one_class: res.data.one_class,
        two_class: res.data.two_class,
        cover: res.data.cover,
        trade_alias: res.data.trade_alias,
        one_class_name: res.data.one_class_name,
        two_class_name: res.data.two_class_name,
        price_type: res.data.price_type,
        true_price: res.data.true_price,
        unit_name: res.data.unit_name,
        unitindex: res.data.unit,
        str: res.data.abbreviation,
        dingjia: res.data.price_type == 1 ? '时价' : '固定价',
        is_show_detail: res.data.is_show_detail,  //显示详情
        commodity_code: res.data.commodity_code  //产品编码
      })
    });
  },
  startcatelist: function () {
    var that = this;
    app.AppHttp('api/Supplier/CateList', {
      id: app.globalData.loginInfo.id,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data));
      if (res.data.two_class_list.length == 0) {
        that.setData({
          two_class_list: [''],
          one_class_list: res.data.one_class_list,
          one_class_zhname: res.data.one_class_list[0].private_class_name,
          one_class_zh: res.data.one_class_list[0].private_one_class,
          notwo: 1
        })
        return;
      }
      that.setData({
        one_class_list: res.data.one_class_list,
        two_class_list: res.data.two_class_list,
        one_class_zhname: res.data.one_class_list[0].private_class_name,
        two_class_zhname: res.data.two_class_list[0].private_class_name,
        one_class_zh: res.data.one_class_list[0].private_one_class,
        two_class_zh: res.data.two_class_list[0].private_one_class,
        value: [0, 0]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})